package com.jeff.android.lab;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jeff.android.lab.algorithm.AlgorithmMainFragment;
import com.jeff.android.lab.android.AndroidMainFragment;
import com.jeff.android.lab.architecture.ArchitectureMainFragment;
import com.jeff.android.lab.bugs.BugsMainFragment;
import com.jeff.android.lab.compat.CompatMainFragment;
import com.jeff.android.lab.database.DatabaseMainFragment;
import com.jeff.android.lab.jetpack.JetPackMainFragment;
import com.jeff.android.lab.module.anim.AnimMainFragment;
import com.jeff.android.lab.module.annotation.AnnotationMainFragment;
import com.jeff.android.lab.module.cache.CacheMainFragment;
import com.jeff.android.lab.module.media.MediaMainFragment;
import com.jeff.android.lab.module.thirdwidget.ThirdWidgetMainFragment;
import com.jeff.android.lab.module.vlayout.VLayoutFragment;
import com.jeff.android.lab.module.widget.WidgetMainFragment;
import com.jeff.android.lab.util.RouterUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: duff
 * @date: 2019/1/7
 * @since: 1.0.0
 */
public class MainFragment extends Fragment {
    RecyclerView mRecyclerView;

    public static MainFragment instance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_main, container, false);
        return mRecyclerView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        mRecyclerView.setLayoutManager(
                new GridLayoutManager(getContext(), 3));

        BaseQuickAdapter adapter = new BaseQuickAdapter<Module, BaseViewHolder>(R.layout.main_item_module) {
            @Override
            protected void convert(BaseViewHolder helper, final Module item) {
                ImageView iv_icon = helper.getView(R.id.iv_icon);
                TextView tv_module = helper.getView(R.id.tv_module);

                iv_icon.setImageResource(item.res);
                tv_module.setText(item.module);

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ("hierarchy".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), AndroidMainFragment.class);
                        } else if ("anim".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), AnimMainFragment.class);
                        } else if ("vlayout".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), VLayoutFragment.class);
                        } else if ("widget".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), WidgetMainFragment.class);
                        } else if ("3rdwidget".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), ThirdWidgetMainFragment.class);
                        } else if ("architecture".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), ArchitectureMainFragment.class);
                        } else if ("Annotation".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), AnnotationMainFragment.class);
                        } else if ("Database".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), DatabaseMainFragment.class);
                        } else if ("Cache".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), CacheMainFragment.class);
                        } else if ("algorithm".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), AlgorithmMainFragment.class);
                        } else if ("media".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), MediaMainFragment.class);
                        } else if ("bugs".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), BugsMainFragment.class);
                        } else if ("compat".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), CompatMainFragment.class);
                        } else if ("jetpack".equals(item.tag)) {
                            RouterUtil.launchFragmentActivity(getContext(), JetPackMainFragment.class);
                        }
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);

        List<Module> modules = new ArrayList<>();
        modules.add(new Module(R.mipmap.ic_launcher, "Android知识体系", "hierarchy"));
        modules.add(new Module(R.mipmap.ic_launcher, "Android组件", "widget"));
        modules.add(new Module(R.mipmap.ic_launcher, "兼容性", "compat"));
        modules.add(new Module(R.mipmap.ic_launcher, "第三方组件", "3rdwidget"));
        modules.add(new Module(R.mipmap.ic_launcher, "动画", "anim"));
        modules.add(new Module(R.mipmap.ic_launcher, "VLayout", "vlayout"));
        modules.add(new Module(R.mipmap.ic_launcher, "架构", "architecture"));
        modules.add(new Module(R.mipmap.ic_launcher, "注解", "Annotation"));
        modules.add(new Module(R.mipmap.ic_launcher, "数据库", "Database"));
        modules.add(new Module(R.mipmap.ic_launcher, "缓存", "Cache"));
        modules.add(new Module(R.mipmap.ic_launcher, "算法", "algorithm"));
        modules.add(new Module(R.mipmap.ic_launcher, "多媒体", "media"));
        modules.add(new Module(R.mipmap.ic_launcher, "疑难杂症", "bugs"));
        modules.add(new Module(R.mipmap.ic_launcher, "JetPack", "jetpack"));
        adapter.setNewData(modules);
    }

    class Module {
        int res;
        String module;
        String tag;

        public Module(int res, String module) {
            this.res = res;
            this.module = module;
        }

        public Module(int res, String module, String tag) {
            this.res = res;
            this.module = module;
            this.tag = tag;
        }
    }

}
